home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre2.z / postgre2 / doc / implementation / query-tree.me < prev   
Encoding:
Text File  |  1992-08-27  |  27.8 KB  |  860 lines

  1. .\"----------------------------------------------------------------------
  2. .\" FILE
  3. .\"    query-tree.t
  4. .\"
  5. .\" DESCRIPTION
  6. .\"    Specification for the query (parser => planner) and plan 
  7. .\"    (planner => executor) trees
  8. .\"
  9. .\" RCS ID
  10. .\"    $Header: /private/postgres/doc/implementation/RCS/query-tree.me,v 1.1 1990/07/30 13:50:41 kemnitz Exp $
  11. .\"----------------------------------------------------------------------
  12. .ds ar <\\(em\\(em
  13. .nr pp 11
  14. .nr sp 12
  15. .nr tp 12
  16. .nr fp 10
  17. .ll 6.5i
  18. .(l C
  19. .sz \n(sp
  20. .ft B
  21. QUERY TREE / QUERY PLAN SPECIFICATION
  22. (Printed: \*(td)
  23. .ft R
  24. .)l
  25. .sp
  26. .sz \n(pp
  27. .sp
  28. For notational purposes, anything appearing in italics is defined
  29. elsewhere in this document.
  30. Anything in bold face surrounded by quotes appears explicitly within a
  31. runtime query tree/plan as a LISP string.
  32. Anything appearing as a list is represented literally as a LISP list.
  33. [] indicates something that is optional; {} indicates 0 or more.
  34. .sp
  35. .he 'Query Tree / Query Plan Specification''%'
  36. .sh 1 "Query Tree Representation"
  37. .sp
  38. The parser will produce the following query tree for each query that
  39. requires optimization:
  40. .(l
  41. (\fIRoot TargetList Qualification\fR)
  42. .)l
  43. Each of the above three components is a list containing further information.
  44. .sh 2 "Root"
  45. .sp
  46. \fBRoot\fR looks like:
  47. .(l
  48. (\fINumLevels CommandType ResultRelation RangeTable Priority RuleInfo\fR)
  49. .)l
  50. This list contains information required by the planner as well as the
  51. executor.
  52. The planner itself does not modify any of these elements,
  53. although some of its preprocessing routines may change portions of 
  54. root before it is seen by the executor.
  55. .sh 3 "NumLevels"
  56. .sp
  57. \fBNumLevels\fR indicates the maximum attribute nesting in a query.
  58. That is, it indicates the maximum number of \*(lqnested dot\*(rq
  59. fields found in any variable in that query.
  60. For example, a query without range variables such as
  61. .(l
  62. retrieve (x = 1 + 2)
  63. .)l
  64. will have \fBNumLevels\fR = 0,
  65. a query such as 
  66. .(l
  67. retrieve (pinhead.name) where pinhead.saying = "Yow!!!"
  68. .)l
  69. that contains normal range variables will have \fBNumLevels\fR = 1,
  70. a query such as 
  71. .(l
  72. retrieve (sorority.all) where sorority.members.name = "Marti"
  73. .)l
  74. will have \fBNumLevels\fR = 2,
  75. and so on.
  76. .sh 3 "CommandType"
  77. .sp
  78. \fBCommandType\fR indicates what kind of query is being executed.
  79. \fBCommandType\fR will be a single symbol from:
  80. .(l
  81. retrieve append replace delete
  82. .)l
  83. (other POSTQUEL commands are not optimized, so the planner should
  84. never see them).
  85. .sh 3 "ResultRelation"
  86. .sp
  87. \fBResultRelation\fR specifies the target relation of a 
  88. \*(lqretrieve\*(rq
  89. or the update relation for
  90. \*(lqappend\*(rq, \*(lqreplace\*(rq, and \*(lqdelete\*(rq.
  91. It may take one of three forms:
  92. .np
  93. If the query is a \*(lqretrieve\*(rq and no result relation is
  94. specified, then \fBResultRelation\fR is nil.
  95. .np
  96. If the query is a \*(lqretrieve into\*(rq, then \fBResultRelation\fR
  97. is a list containing a string representing the result relation's name
  98. and a string from:
  99. .(l
  100. \fB\*(lqHEAVY\*(rq \*(lqLIGHT\*(rq \*(lqNONE\*(rq\fR
  101. .)l
  102. which represents the level of archiving which has been specified for the
  103. result relation.
  104. .np
  105. If the query is an update, \fBResultRelation\fR will always be the
  106. index into the \fIrangetable\fR corresponding to the relation to be
  107. updated.
  108. .sh 3 "RangeTable"
  109. .sp
  110. \fBRangeTable\fR is a list containing a \fIrangetable entry\fR for
  111. each instance of a relation (i.e., range variable) in a query.  Every
  112. \fIrelid\fR and \fIvarno\fR within the query tree, and almost every one
  113. within the resulting query plan, is an integer index within the
  114. query's rangetable.  This index is 1-based (e.g., var nodes that refer
  115. to the first relation in the rangetable are given varno ``1'').
  116. .sp
  117. A rangetable entry looks like:
  118. .(l
  119. ( \fIRelationName RelationOID TimeRange Flags RuleLocks\fR )
  120. .)l
  121. \fBRelationName\fR is a string corresponding to the relation's catalog
  122. name, \fBRelationOID\fR is an integer corresponding to the OID of the
  123. RELATION relation tuple that describes the given relation,
  124. \fBTimeRange\fR is an internal structure that describes the historical
  125. time span over which this rangetable entry is valid,
  126. \fBFlags\fR is a list of zero or more symbols from:
  127. .(l
  128. archive inheritance union version
  129. .)l
  130. that specifies special treatment from the planner, and 
  131. \fBRuleLocks\fR holds the rule lock information for that relation
  132. (since this is generated by a planner preprocessor, the parser simply
  133. puts nil here).
  134. In the case of archival, inheritance, union and version queries,
  135. the planner generates an \fBAPPEND\fR query plan node (see below) that
  136. contains a set of rangetable entries which must be substituted in turn
  137. for this rangetable entry, as well as separate query plans to be
  138. executed.
  139. .sh 3 "Priority"
  140. .sp
  141. \fBPriority\fR is simply an integer from 0..7 that indicates the
  142. query priority.  Only rule plans can have priorities greater than 0.
  143. .sh 3 "RuleInfo"
  144. .sp
  145. If the query is part of a \*(lqdefine rule\*(rq query, \fBRuleInfo\fR
  146. is a containing a \fIrule identifier\fR and exactly one symbol from:
  147. .(l
  148. always once never
  149. .)l
  150. The rule identifier is an object identifier from the RULE relation;
  151. the symbol is the tag associated with the rule definition.
  152. For example, \fBRuleInfo\fR might look like:
  153. .(l
  154. ( 387457893 always )
  155. .)l
  156. The rule identifier will be filled in by a traffic cop or planner
  157. preprocessor routine.
  158. For all other queries, \fBRuleInfo\fR is nil.
  159. .sh 2 "TargetList"
  160. .sp
  161. A targetlist describes the structure of a tuple
  162. and consists of a list of (\fIresdom expr\fR) pairs, one for
  163. each attribute in the tuple to be constructed.
  164. A \fBresdom\fR (result domain) node contains information about the
  165. attribute, and
  166. an \fBexpr\fR (expression) describes how to set the value of the
  167. attribute.
  168. .sp
  169. The initial targetlist from the query tree describes what the final
  170. result tuples should look like (targetlists are also used in
  171. query plans to describe intermediate results such as scan projections,
  172. join results, and temporaries).
  173. In the case of the delete command, the initial targetlist is always nil.
  174. For the other update queries, the planner's preprocessor fills in any
  175. missing attributes that the user has not specified, either with
  176. constants corresponding to the default values of the missing
  177. attributes\**
  178. .(f
  179. \** If no default value exists for an attribute type, the attribute
  180. will have a null value.
  181. .)f
  182. (for \*(lqappend\*(rq) or with variables corresponding to
  183. the unchanged attributes of a tuple (for \*(lqreplace\*(rq).
  184. .sp
  185. The structure of the \fBresdom\fR node is described below;
  186. an \fBexpr\fR can consist of a combination of nodes:
  187. .(l
  188. \fIexpr\fR = \fIvar\fR | \fIconst\fR | \fIparam\fR | (\fIoper expr\fR [\fIexpr\fR]) | (\fIfunc\fR {\fIexpr\fR})
  189. .)l
  190. .sp
  191. The structures of the different nodes in an expr are described
  192. below, but the function of the nodes may be briefly summarized as:
  193. .nr zz \n(ii
  194. .nr ii 6m
  195. .sp
  196. .ip \fIvar\fR
  197. Refer to some relation attribute entry which corresponds to the target
  198. list entry (i.e., the contents of a relation's tuple).
  199. .ip \fIconst\fR
  200. Constant values.
  201. .ip \fIparam\fR
  202. Used as parameters \(em placeholders for constants \(em within a
  203. query.  The planner treats them as if they are constants of unknown
  204. value (i.e., null constants) for optimization purposes.
  205. .ip \fIoper\fR
  206. Describes the system- or user-defined unary or binary operator that
  207. will be applied to the argument exprs to produce a new value.
  208. .ip \fIfunc\fR
  209. Correspond to calls to system- or user-defined functions, which
  210. may have zero or more arguments.
  211. .nr ii \n(zz
  212. .lp
  213. Example:  ((\fIresdom var\fR) (\fIresdom\fR (\fIoper var var\fR)))
  214. .br
  215. is a targetlist with two attributes, where the second element involves
  216. a computation between two tuple attributes.
  217. .sh 2 "Qualification"
  218. .sp
  219. A qualification consists of restriction and join clauses which are
  220. evaluated before the final result is returned.
  221. .sp
  222. The qualifications produced by the parser are not normalized in any
  223. way.
  224. That is, the parsed qualification will be an arbitrary boolean
  225. expression:
  226. .(l
  227. \fIqualification\fR = ({\fIqual\fR})
  228. \fIqual\fR = \fIexpr\fR | (\*(lq\fBNOT\fR\*(rq \fIqual\fR) | (\*(lq\fBOR\fR\*(rq \fIqual \fR{\fIqual\fR}) | (\*(lq\fBAND\fR\*(rq \fIqual \fR{\fIqual\fR})
  229. .)l
  230. .sp
  231. The qualification supplied to the planner must be normalized in
  232. certain ways.\**
  233. .(f
  234. \** This normalization occurs in yet another preprocessing module.
  235. .)f
  236. A qualification must be specified in conjunctive normal form.
  237. Furthermore, if it is at all possible, all \*(lqnot\*(rqs are removed
  238. and all variables are placed on the left hand side of the clause
  239. operator.
  240. If it is not possible to remove a \*(lqnot\*(rq (perhaps because a
  241. negator is unavailable),
  242. then the \*(lqnot\*(rq must be pushed to the innermost comparison,
  243. e.g.,
  244. \*(lq(not ((a = b) and (c = d)))\*(rq
  245. might become
  246. \*(lq(not (a = b) or not (c = d))\*(rq
  247. if the operator \*(lq!=\*(rq does not exist for the appropriate types.
  248. .sp
  249. A qualification as seen by the planner is:
  250. .(l
  251. \fIqualification\fR = ({\fIqual\fR | \fIorqual\fR})
  252. \fIqual\fR = \fIexpr\fR | (\*(lq\fBNOT\fR\*(rq \fIexpr\fR)
  253. \fIorqual\fR = (\*(lq\fBOR\fR\*(rq \fIqual qual \fR{\fIqual\fR})
  254. .)l
  255. .sp
  256. Example: If a user specifies the following clause:
  257. .(l
  258. not (r.f = 1) or (r.f2 > 1 and 2 > r.f2),
  259. .)l
  260. then in conjunctive normal form with \*(lqnot\*(rqs removed
  261. and variables on the left hand side, we have the following equivalent clause:
  262. .(l
  263. (r.f != 1 or r.f2 > 1) and (r.f != 1 or r.f2 < 2)
  264. .)l
  265. In LISP form, it would look like:
  266. .(l
  267. ((or (!= r.f 1) (> r.f2 1)) (or (!= r.f 1) (< r.f2 2)))
  268. .)l
  269. .sp
  270. A set of macros for manipulating clause entries is contained in:
  271. .(l
  272. ~postgres/src/planner/clauses.l
  273. .)l
  274. Additional parse tree manipulation routines are in:
  275. .(l
  276. ~postgres/src/planner/nodeDefs.l
  277. .)l
  278. .bp
  279. .sh 1 "Query Plan Representation"
  280. .sp
  281. A query plan is represented as a node.
  282. Every plan node contains the following slots:
  283. .(l
  284. .ft B
  285. state
  286. qptargetlist
  287. lefttree
  288. righttree
  289. .ft R
  290. .)l
  291. Thus, while a query plan is externally a single object, it may in fact
  292. constitute a tree of plan nodes because of the child tree fields.
  293. .sp
  294. A \fIplan\fP consists of \fIsubplans\fP interconnected together with
  295. \fBRESULT\fR,
  296. \fBEXISTENTIAL\fP,
  297. and
  298. \fBAPPEND\fR
  299. nodes.  These nodes may also have each other as children.
  300. .sp
  301. \fBRESULT\fR nodes interconnect plan levels.
  302. The planner generates subqueries for all variables which operate at a
  303. given level of nesting (that is, all non-nested variables are handled
  304. in one plan level, all variables of the form \*(lqfoo.bar.baz\*(rq are
  305. handled in another, and so on).
  306. Thus, \fBRESULT\fR nodes indicate which attributes should be selected
  307. from lower levels in the plan tree.
  308. If a \fBRESULT\fR node has subtrees,
  309. then the left tree always is a \fIsubplan\fR,
  310. and the right tree is a \fIplan\fR for processing the
  311. (more deeply nested) remainder of the query.
  312. It is also possible for a \fBRESULT\fR node to have only a single left
  313. subtree.
  314. This occurs when the \fBRESULT\fR node is needed solely for storing
  315. relation-level clauses.
  316. A query plan can be a single \fBRESULT\fR node with no subtrees if the
  317. query contains no variables.
  318. .sp
  319. \fBEXISTENTIAL\fP nodes also connect subplans.
  320. The left subtree of an \fBEXISTENTIAL\fP node is a special
  321. subplan which determines whether the existential members
  322. of the subplan qualification are all true or not.
  323. The right subtree is the actual query plan for the
  324. subquery (minus the existential qualification clauses).
  325. \fBEXISTENTIAL\fP nodes may also have one (left) or zero subtrees.
  326. .sp
  327. \fBAPPEND\fR nodes indicate that the executor should use the subplans it
  328. contains sequentially, returning the appropriate tuples in a seamless
  329. manner (at least as far as the external interface is concerned).
  330. \fBAPPEND\fR nodes may be thought of as taking the place of N
  331. scan nodes, each of which are substituted for the \fBAPPEND\fR node in turn.
  332. .sp
  333. A \fIsubplan\fR is an access plan for processing the (current)
  334. topmost level of attributes in a query and consists of join and scan
  335. nodes.
  336. .sp
  337. Joins are represented such that the left tree is the outer join relation
  338. and the right tree is the inner join relation.
  339. The three types of join nodes are:
  340. .(l
  341. .ft B
  342. NESTLOOP
  343. MERGESORT
  344. HASHJOIN
  345. .ft R
  346. .)l
  347. The two types of scan nodes are:
  348. .(l
  349. .ft B
  350. SEQSCAN
  351. INDEXSCAN
  352. .ft R
  353. .)l
  354. .sp
  355. Lastly, there are two types of nodes which involve the creation of
  356. temporary relations:
  357. .(l
  358. .ft B
  359. SORT
  360. HASH
  361. .ft R
  362. .)l
  363. If a stream of tuples must be sorted into a temporary prior
  364. to join processing, its parent is a \fBSORT\fR node and the parent of
  365. the \fBSORT\fR node is a \fBSEQSCAN\fR node:
  366. .(l
  367. <some-join> \fB\*(ar SEQSCAN \*(ar SORT \*(ar\fR <some-scan>
  368. .)l
  369. Similarly, if the relation is to be hashed, its corresponding scan
  370. node's parent is a \fBHASH\fR node.
  371. .(l
  372. <some-join> \fB\*(ar SEQSCAN \*(ar HASH \*(ar\fR <some-scan>
  373. .)l
  374. Example: A \*(lqretrieve\*(rq involving a single mergesort join that
  375. uses an index as an ordered inner path and a sorted outer path might
  376. look like:
  377. .(l
  378. .ft B
  379. MERGE    \*(ar SEQSCAN \*(ar SORT \*(ar SEQSCAN
  380. SORT        \*(ar INDEXSCAN
  381. .ft R
  382. .)l
  383. .sp
  384. The same idea applies to sorts in general.  The node is simply
  385. preceded by a \fBSORT\fR and then a \fBSEQSCAN\fR node in the plan
  386. tree.  However, if a retrieve result is to be sorted into a relation,
  387. the \fBSEQSCAN\fR node is omitted.
  388. .sp
  389. Example: The top level nodes for a plan that returns a set of tuples
  390. that must be sorted into a particular order might look like:
  391. .(l
  392. \fBRESULT    \*(ar SORT \*(ar\fR ...rest of plan...
  393. .)l
  394. .bp
  395. .sh 1 "Primitive Nodes"
  396. .sp
  397. The following is a description of the primitive nodes, which are used
  398. in both the input query tree and the final query plan.
  399. .sp
  400. Each type of node is implemented as a defstruct vector.  To access
  401. slots within a node, use:
  402. .(l
  403. (get_\fIslotname node\fR)
  404. .)l
  405. where \fIslotname\fR is the desired slot name as described in this
  406. section and \fInode\fR is an appropriate defstruct node.
  407. .sp
  408. To set a slot field, use:
  409. .(l
  410. (set_\fIslotname node new-slot-value\fR)
  411. .)l
  412. For details see
  413. .(l
  414. ~postgres/src/planner/nodeDefs.l
  415. .)l
  416. .sp
  417. .sh 2 "RESDOM"
  418. .ip "1)"
  419. \fBresno\fR - result domain number.  This is equivalent to the
  420. attribute number for a (temporary or result) tuple.  Just as attribute
  421. numbers being at 1, a \fBresno\fR of 1 denotes the first attribute in
  422. a targetlist.
  423. .ip "2)"
  424. \fBrestype\fR - either:
  425. .br
  426. (1) the OID corresponding to the type of the result, or
  427. .br
  428. (2) *UNION-TYPE-ID*, an identifier for a type internal to the planner
  429. that describes multi-dot attributes (since it cannot determine the
  430. final attribute type until the nesting is eliminated by the executor).
  431. .ip "3)"
  432. \fBreslen\fR - length of a domain element in bytes (-1 if it is
  433. variable length).
  434. .ip "4)"
  435. \fBresname\fR - result domain name, either user-specified or the
  436. corresponding name of an attribute (if applicable).
  437. .ip "5)"
  438. \fBreskey\fR - ordinality of the result domain as a sort or hash key,
  439. e.g.,
  440. 0 if the domain won't be included as a sort or hash key;
  441. 1 if the domain is the first key,
  442. 2 if it is the second,
  443. and so on.
  444. .ip "6)"
  445. \fBreskeyop\fR - OID of the operator on which \fBreskey\fR will be
  446. sorted or hashed.
  447. .sp
  448. .lp
  449. To create a \fBRESDOM\fR node:
  450. .sp
  451. (make_resdom \fIresno restype reslen resname reskey reskeyop\fR)
  452. .sp
  453. .sh 2 "VAR"
  454. .ip "1)"
  455. \fBvarno\fR - From the parser's standpoint, \fBvarno\fR is simply the
  456. index into the rangetable corresponding to the relation an attribute
  457. belongs to.
  458. .sp
  459. For the query processor, \fBvarno\fR may take on one of three values
  460. depending on the attribute entry under consideration.
  461. .br
  462. (1) If \fBVAR\fR belongs to the relation currently being scanned
  463. (either a cataloged or temporary relation, but not a materialized
  464. one), \fBvarno\fR is still the \fIindex into the rangetable\fP
  465. corresponding to the relation being scanned.
  466. .br
  467. (2) If \fBVAR\fR is associated with some join node, varno is the
  468. \fIname of the temporary join relation\fP accessed.  If \fBVAR\fR
  469. originates from the outer join relation, \fBvarno\fR =
  470. \fB\*(lqOUTER\*(rq\fR.  If it originated from the inner join relation,
  471. \fBvarno\fR = \fB\*(lqINNER\*(rq\fR.
  472. .br
  473. (3) For \fBVAR\fRs corresponding to entries within relations that are
  474. materialized from a POSTQUEL field or attributes from some previous
  475. nesting level, \fBvarno\fR is a \fIreference pair\fP, i.e., a list
  476. (\fIlevelnum resno\fR), that corresponds to the attribute entry where
  477. either the query field or desired value can be found.  \fIlevelnum\fR
  478. refers to the processing level number (where \fI0\fR is the topmost),
  479. and \fIresno\fR refers to the result domain number within the tuple
  480. where the entry is stored.
  481. .sp
  482. Therefore, whether a relation should be materialized is implicit in
  483. the \fBvarno\fR value and information in the attribute entry
  484. corresponding to the query field.
  485. .ip "2)"
  486. \fBvarattno\fR - as with \fBvarno\fR, the meaning of this field
  487. depends on the node's position in the plan tree.
  488. .br
  489. (1) For attributes at the topmost nesting level, \fBvarattno\fR
  490. corresponds to the \fIattribute number\fR within a relation (or domain
  491. number within a tuple if this is a join tuple \(em that is, if
  492. \fBvarno\fR = \fB\*(lqOUTER\*(rq\fR or \fB\*(lqINNER\*(rq\fR).
  493. .br
  494. (2) If the attribute corresponds to some attribute from a previous
  495. nesting level, \fBvarattno\fR = \fInil\fR.
  496. .br
  497. (3) For nested attributes, the value of \fBvarattno\fR is a
  498. \fIstring\fR identical to the attribute name since the attribute
  499. number will not be known until the POSTQUEL field is materialized.
  500. Note that at the lowest nesting level, \fBvarattno\fR may be
  501. \*(lq\fBALL\fR\*(rq (again, since neither the parser nor the planner
  502. know what the attributes of the field's parent are \(em it must be
  503. determined at run-time by the query processor when the appropriate
  504. POSTQUEL field is retrieved).
  505. .ip "3)"
  506. \fBvartype\fR - either:
  507. .br
  508. (1) the OID of the type of the attribute referred to by \fBvarattno\fR,
  509. or
  510. .br
  511. (2) *UNION-TYPE-ID*
  512. .ip "4)"
  513. \fBvardotfields\fR - list of attribute names beyond the first nesting
  514. level.
  515. .br
  516. (\fIThis field is internal to the parser and planner\fR)
  517. .ip "5)"
  518. \fBvararrayindex\fR - array index of a variable, nil if this is not an
  519. array attribute.
  520. .br
  521. Since arrays can only contain primitive types, only the bottommost
  522. attribute in a nesting will be array indexed.  The parser will set
  523. the \fBvararrayindex\fR field if a variable contains an array index.
  524. The planner will only set this field if the query processor is to
  525. retrieve an array element within some attribute entry.
  526. .ip "6)"
  527. \fBvarid\fR - a list containing \fBvarno\fR and \fBvarattno\fR, as
  528. well as \fBvardotfields\fR and \fBvararrayindex\fR (if the latter two
  529. exist).
  530. .br
  531. (\fIThis field is internal to the planner\fR)
  532. .sp
  533. .nf
  534. Example: w.x.y.z[a]
  535. .sp
  536. .nf
  537. The parser will set the following fields:
  538. .sp
  539. \fBvarno\fR = identifier of w, \fBvarattno\fR = attribute number of x, 
  540. .br
  541. \fBvardotfields\fR = (y z), \fBvararrayindex\fR = a,
  542. .br
  543. \fBvarid\fR = (\fBvarno varattno\fR y z (a)),
  544. \fBvartype\fR = type of attribute x
  545. .fi
  546. .sp
  547. .lp
  548. To create a \fBVAR\fR node:
  549. .sp
  550. (make_var \fIvarno varattno vartype vardotfields vararrayindex varid\fR)
  551. .sp
  552. .sh 2 "OPER"
  553. .ip "1)"
  554. \fBopno\fR - for the parser, \fBopno\fR is the OID of the operator to
  555. which this node corresponds.  For the executor, \fBopno\fR is the OID
  556. of the operator \fIprocedure\fR.  The conversion occurs in the planner
  557. (which needs the operator OID to determine selectivities).
  558. .ip "2)"
  559. \fBoprelationlevel\fR - t if an operator is a relation level operator.
  560. .ip "3)"
  561. \fBopresulttype\fR - OID of the result type of this particular
  562. operator.
  563. .sp
  564. .lp
  565. To create a \fBOPER\fR node:
  566. .sp
  567. (make_oper \fIopno oprelationlevel opresulttype\fR)
  568. .sp
  569. .sh 2 "CONST"
  570. .ip "1)"
  571. \fBconsttype\fR - OID of the constant's type.
  572. .ip "2)"
  573. \fBconstlen\fR - length in bytes (-1 if it is a variable-length type).
  574. .ip "3)"
  575. \fBconstvalue\fR - actual value of the constant (i.e., the internal
  576. representation).
  577. .br
  578. \fBconstvalue\fR is nil if the constant is null.  Objects represented
  579. by pointers must appear as palloc'ed LISP vectori-bytes.
  580. .ip "4)"
  581. \fBconstisnull\fR - t if the constant has the null value.
  582. .br
  583. This is set by the planner when filling in \*(lqreplace\*(rq and
  584. \*(lqappend\*(rq targetlist entries which are unspecified by the user.
  585. If the typdefault field of the \fBTYPE\fR relation is not specified,
  586. \fBconstisnull\fR is set to t and \fBconstvalue\fR is set to nil.
  587. .sp
  588. .lp
  589. To create a \fBCONST\fR node:
  590. .sp
  591. (make_const \fIconsttype constlen constvalue constisnull\fR)
  592. .sp
  593. .sh 2 "PARAM"
  594. .ip "1)"
  595. \fBparamid\fR - either:
  596. .br
  597. (1) a fixnum corresponding to the parameter name for constant value
  598. substitution, e.g., \*(lq1\*(rq in the case of \*(lq$1\*(rq
  599. .br
  600. (2) a string corresponding to the attribute name for relation name
  601. substitution, e.g, \*(lqattname\*(rq in the case of
  602. \*(lq$.attname\*(rq
  603. .sp
  604. .lp
  605. To create a \fBPARAM\fR node:
  606. .sp
  607. (make_param \fIparamid\fR)
  608. .sp
  609. .sh 2 "FUNC"
  610. .ip "1)"
  611. \fBfuncid\fR - OID of the function to which this node corresponds.
  612. .ip "2)"
  613. \fBfunctype\fR - OID of the type of the function's return value.
  614. .sp
  615. .lp
  616. To create a \fBFUNC\fR node:
  617. .sp
  618. (make_func \fIfuncid functype\fR)
  619. .bp
  620. .sh 1 "Plan Nodes"
  621. .sp
  622. The following is a description of each type of plan node.  Each type
  623. of node is implemented as a defstruct vector.  To access slots within
  624. a node, use:
  625. .(l
  626. (get_\fIslotname node\fR)
  627. .)l
  628. where \fIslotname\fR is the desired slot name as described in this
  629. section and \fInode\fR is an appropriate defstruct node.
  630. .sp
  631. To set a slot field, use:
  632. .(l
  633. (set_\fIslotname node new-slot-value\fR)
  634. .)l
  635. For details see
  636. .(l
  637. ~postgres/src/planner/nodeDefs.l
  638. .)l
  639. .sp
  640. .sh 2 "RESULT"
  641. .ip "1)"
  642. \fBstate\fR - used by the query executor to store runtime-specific
  643. information.
  644. .ip "2)"
  645. \fBqptargetlist\fR - the target list for a sublevel of the query; the
  646. attributes in node \fBRESULT\fI(i)\fR are derived from values returned
  647. by \fIsubplan(i)\fR, and either \fBRESULT\fI(i+1)\fR or
  648. \fIsubplan(i+1)\fR, depending on whether or not there is an i+1'th
  649. nesting level.
  650. .ip "3)"
  651. \fBlefttree\fR - \fIsubplan(i)\fR.
  652. .ip "4)"
  653. \fBrighttree\fR - either \fBRESULT\fI(i+1)\fR or \fIsubplan(i+1)\fR,
  654. depending on whether or not there are deeper nesting levels.
  655. .ip "5)"
  656. \fBresrellevelqual\fR - list of any relation level clauses that must
  657. be satisfied (e.g., \*(lqR1.f1 = R2.f2\*(rq or \*(lqfoo.bar = 2\*(rq)
  658. .ip "6)"
  659. \fBresconstantqual\fR - a list containing all qualifications which
  660. contain no var nodes.
  661. .br
  662. This field will only be set at the topmost \fBRESULT\fR node.  These
  663. qualifications should always be tested first at runtime; if they are
  664. false, then the rest of the query plan need not be processed because
  665. no tuples will be returned.
  666. .sp
  667. To create a \fBRESULT\fR node:
  668. .sp
  669. (make_result \fIqptargetlist resrellevelqual lefttree righttree\fR)
  670. .sp
  671. .sh 2 "EXISTENTIAL"
  672. .ip "1)"
  673. \fBstate\fR
  674. .ip "2)"
  675. \fBqptargetlist\fR - always nil.
  676. .ip "3)"
  677. \fBlefttree\fR - subplan for the existential query.
  678. .br
  679. This is planned as a \*(lqretrieve\*(rq with a null target list.
  680. .ip "4)"
  681. \fBrighttree\fR - subplan for the non-existential query.
  682. .sp
  683. .lp
  684. To create a \fBEXISTENTIAL\fR node:
  685. .sp
  686. (make_existential \fIlefttree righttree\fR)
  687. .sp
  688. .sh 2 "APPEND"
  689. .ip "1)"
  690. \fBstate\fR
  691. .ip "2) - 4) \fBqptargetlist\fR, \fBlefttree\fR, \fBrighttree\fR - always nil"
  692. .ip "5)"
  693. \fBplans\fR - a list of plans which are to be executed sequentially.
  694. .ip "6)"
  695. \fBrelid\fR - the rangetable index of the variable which caused this
  696. node to be formed.  For example, if this is a query that uses the
  697. \fBfrom\fR clause \*(lqfrom p in person*\*(rq, then this would contain
  698. the rangetable index of the entry for \*(lqperson\*(rq.
  699. .ip "7)"
  700. \fBrtentries\fR - a list of rangetable entries which must be
  701. substituted into the executor's rangetable in conjunction with
  702. execution of the plans stored in the \fBplans\fR field.
  703. .ip "8)"
  704. \fBflags\fR - a symbol from 
  705. \fBarchive\fR, \fBinheritance\fR, \fBunion\fR, or \fBversion\fR
  706. that specifies how the executor should execute each entry in the
  707. \fBplans\fR.
  708. .sp
  709. .lp
  710. To create a \fBAPPEND\fR node:
  711. .sp
  712. (make_append \fIplans relid rtentries flags\fR)
  713. .sp
  714. .sh 2 "NESTLOOP"
  715. .ip "1)"
  716. \fBstate\fR
  717. .ip "2)"
  718. \fBqptargetlist\fR - attributes to be retrieved into the join result
  719. from attributes in the inner and outer join relations.
  720. .ip "3)"
  721. \fBlefttree\fR - outer join path.
  722. .ip "4)"
  723. \fBrighttree\fR - inner join path.
  724. .ip "5)"
  725. \fBqpqual\fR - a list of join clauses to be satisfied.
  726. .sp
  727. .lp
  728. To create a \fBNESTLOOP\fR node:
  729. .sp
  730. (make_nestloop \fIqptargetlist qpqual lefttree righttree\fR)
  731. .sp
  732. .sh 2 "HASHJOIN"
  733. .ip "1) - 5) from above"
  734. .ip "6)"
  735. \fBmergehashclauses\fR - join clauses to be used in performing the
  736. hash join; if there are multiple clauses, the clauses are arranged in
  737. the order of the hash keys, from primary downward.
  738. .br
  739. Note: These clauses are not contained in \fBqpqual\fR above to avoid
  740. redundant checking.
  741. .sp
  742. .lp
  743. To create a \fBHASHJOIN\fR node:
  744. .sp
  745. (make_hashjoin \fIqptargetlist qpqual mergehashclauses lefttree righttree\fR)
  746. .sp
  747. .sh 2 "MERGESORT"
  748. .ip "1) - 5) from above"
  749. .ip "6)"
  750. \fBmergehashclauses\fR - join clauses to be used in performing the
  751. merge join; if there are multiple clauses, the clauses are arranged in
  752. the order of the merge keys, from the primary ordering key downward.
  753. .br
  754. (Again, these clauses are not contained in \fBqpqual\fR)
  755. .ip "7)"
  756. \fBmergesortop\fR - OID of the operator procedure used to merge the
  757. tuples.
  758. .sp
  759. .lp
  760. To create a \fBMERGESORT\fR node:
  761. .sp
  762. (make_mergesort \fIqptargetlist qpqual mergehashclauses mergesortop lefttree righttree\fR)
  763. .sp
  764. .sh 2 "SEQSCAN"
  765. .ip "1)"
  766. \fBstate\fR
  767. .ip "2)"
  768. \fBqptargetlist\fR - attributes to be retrieved into scan result.
  769. .ip "3)"
  770. \fBlefttree\fR - either nil, a \fBSORT\fR node, or a \fBHASH\fR node.
  771. .ip "4)"
  772. \fBrighttree\fR - always nil.
  773. .ip "5)"
  774. \fBqpqual\fR - list of restriction clauses on the relation.
  775. .ip "6)"
  776. \fBscanrelid\fR - either:
  777. .br
  778. (1) an index into the rangetable corresponding to the relation to be
  779. scanned, or
  780. .br
  781. (2) a reference to an attribute from a previous level that corresponds
  782. to a relation to be materialized from some POSTQUEL field.
  783. .sp
  784. .lp
  785. To create a \fBSEQSCAN\fR node:
  786. .sp
  787. (make_seqscan \fIqptargetlist qpqual scanrelid lefttree\fR)
  788. .sp
  789. .sh 2 "INDEXSCAN"
  790. .ip "1) - 2) from above"
  791. .ip "3) - 4) \fBlefttree\fR, \fBrighttree\fR - always nil.
  792. .ip "5) - 6) from above"
  793. .ip "7)"
  794. \fBindxid\fR - list of the OIDs of all indices to be used to scan a
  795. relation: (OID1 OID2 ... ).  The idea here is that more than one index
  796. may be used to scan a relation.  For example, if we have the clause
  797. \*(lqR.foo = foo or R.foo = foobar\*(rq, the first index might use
  798. \*(lqfoo\*(rq as a key and the second could use \*(lqfoobar\*(rq.
  799. Currently, this tactic is only used as just described (in conjunction
  800. with \*(lqor\*(rq clauses).
  801. .ip "8)"
  802. \fBindxqual\fR - list of qualifications to be used in conjunction with
  803. each index (e.g., ((qual1) (qual2)), where qual1 corresponds to ID1
  804. and qual2 corresponds to ID2).  For multi-key indices, subclauses are
  805. arranged in the order of the index keys.
  806. .br
  807. Note, again, that the clauses here are not contained in the
  808. \fBqpqual\fR field above to avoid redundant checking.
  809. .sp
  810. If \fBindxqual\fR is nil, but \fBindxid\fR isn't, then the entire
  811. relation is to be scanned starting at the first key of the index.
  812. This is used when the ordering of an indexscan is needed for a
  813. mergesort.
  814. .sp
  815. If an index is defined on an inner join relation, \fBindxqual\fR may
  816. be a join clause.  If this is the case, the query processor must
  817. substitute values for the outer relation at runtime.  The outer join
  818. attribute is identified by its relative position of within the
  819. resulting tuple of the outer join relation.  This type of
  820. qualification is also omitted from \fBqpqual\fR above to avoid
  821. redundant checking.
  822. .sp
  823. .lp
  824. To create a \fBINDEXSCAN\fR node:
  825. .sp
  826. (make_indexscan \fIqptargetlist qpqual scanrelid indxid indxqual\fR)
  827. .sp
  828. .sh 2 "SORT"
  829. .ip "1)"
  830. \fBstate\fR
  831. .ip "2)"
  832. \fBqptargetlist\fR - targetlist for the tuples which are to be placed
  833. into the temporary relation.
  834. .ip "3)"
  835. \fBlefttree\fR - plan node whose result is to be sorted.
  836. .ip "4)"
  837. \fBrighttree\fR - always nil.
  838. .ip "5)"
  839. \fBtempid\fR - either:
  840. .br
  841. (1) a string corresponding to a user-specified result relation, or 
  842. .br
  843. (2) *TEMP-RELATION-ID* (a special identifier indicating a temporary
  844. relation).
  845. .ip "6)"
  846. \fBkeycount\fR - the number of sort keys.
  847. .sp
  848. .lp
  849. To create a \fBSORT\fR node:
  850. .sp
  851. (make_sort \fIqptargetlist tempid lefttree\fR)
  852. .sp
  853. .sh 2 "HASH"
  854. .ip "1) - 6) from above"
  855. .sp 2
  856. .lp
  857. To create a \fBHASH\fR node:
  858. .sp
  859. (make_hash \fIqptargetlist tempid lefttree\fR)
  860.